import requests
import json

ip1 = input("请输入你的ip地址：")
r = requests.get(f'https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip1}')
con = r.text
print(type(con))
#json转字典
new = json.loads(con)
print(new)
print(f"该{ip1}所属为：\n")

print("国家：", new.get("data").get("country"))
print("省份：", new.get("data").get("city"))
print("isp：", new.get("data").get("isp"))
with open('hw2_1.txt', 'w') as f:

    f.write(f"该{ip1}所属为：\n")
    f.write(new.get("data").get("country")+"\n")
    f.write(new.get("data").get("city")+"\n")
    f.write(new.get("data").get("isp")+"\n")

